/*
 * 	<pre>
 *	EasyPeas.
 *  Copyright (C) 2007 S Owen
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Jul 24, 2007
 * ObjectCreatorFactory.java
 * </pre>
 */
package org.easypeas.objectcreation;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.easypeas.EasyPeasConstants;

/**
 * A Factory class that provides an instance of an ObjectCreator, whose
 * responsibility is to create instances of classes based upon the class name.<br>
 * 
 * @author S Owen
 * 
 */
public class ObjectCreatorFactory {
	private static Logger logger = Logger.getLogger("org.easypeas.objectcreation.ObjectCreatorFactory");

	private static ObjectCreatorFactory instance = new ObjectCreatorFactory();

	/**
	 * Instantiates a new object creator factory. Private to force use of the
	 * getInstance method
	 */
	private ObjectCreatorFactory() {
	};

	/**
	 * Gets the single instance of ObjectCreatorFactory.
	 * 
	 * @return single instance of ObjectCreatorFactory
	 */
	public static ObjectCreatorFactory getInstance() {
		return instance;
	}

	/**
	 * Returns an implemtation of ObjectCreator.<br>
	 * Currently the default ObjectCreator implementation is
	 * {@link ReflectionObjectCreator}<br>
	 * which can be overridden with the Servlet init param
	 * easypeas.objectcreator
	 * 
	 * @return the object creator
	 */
	public ObjectCreator creator() {
		String property = System
				.getProperty(EasyPeasConstants.EASYPEA_OBJECTCREATOR);
		logger.finer("Creating new ObjectCreator for property:" + property);
		ObjectCreator result = null;
		if (property == null) {
			result = new ReflectionObjectCreator();
		} else {
			try {
				result = (ObjectCreator) Class.forName(property).newInstance();
			} catch (Exception e) {
				logger.log(Level.SEVERE,"Error creating overridden ObjectCreator named:"
						+ property, e);
			}
		}
		return result;
	}
}
